package com.bw.unit6

import androidx.recyclerview.widget.GridLayoutManager
import com.bw.unit3.base.BaseActivity
import com.bw.unit6.bean.GoodsBean
import com.bw.unit6.databinding.ActivityGoodsListBinding
import com.bw.unit6.viewmodel.GoodsViewModel
import com.bw.unit6.viewmodel.LoginViewModel
import com.jcodecraeer.xrecyclerview.XRecyclerView.LoadingListener

class GoodsActivity :
    BaseActivity<GoodsViewModel, ActivityGoodsListBinding>(R.layout.activity_goods_list) {

    val id = 0
    var page = 1
    var size = 10
    var mdata = mutableListOf<GoodsBean>()
    lateinit var adapter: GoodsAdapter
    override fun initData() {

        viewModel.liveDataGoods.observe(this) {
            if (page == 0) {
                mdata.clear()
            }
            mdata.addAll(it.data)
            //刷新适配器
            adapter.notifyDataSetChanged()
            //刷新完成，结束动画
            dataBinding.rv.refreshComplete()
        }
    }

    override fun initView() {

        viewModel.getGoodsList(id, page, size)
        dataBinding.rv.layoutManager = GridLayoutManager(this@GoodsActivity, 2)
        adapter = GoodsAdapter(this@GoodsActivity, R.layout.item_goods_layout, mdata)
        //设置适配器
        dataBinding.rv.adapter = adapter
        dataBinding.rv.setLoadingListener(object : LoadingListener {
            override fun onRefresh() {
                //刷新
                page = 1
                viewModel.getGoodsList(id, page, size)
            }

            override fun onLoadMore() {
                //加载更多
                page++
                viewModel.getGoodsList(id, page, size)
            }

        })
    }

}